package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.model.Customer;
import com.orm.analysis.service.GenericService;

import java.util.List;
import java.util.Map;


public class ContactInfoAction extends FrontendAction  {

    private GenericService<ContactInfo> service;
    private GenericService<Address> addressService;
    private ContactInfo contactInfo;
    private List<ContactInfo> contactInfos;
    private List<Address> addressList;
    private Integer[] addressIdList;


    public ContactInfoAction(GenericService<ContactInfo> service, GenericService<Address> addressService) {
        this.service = service;
        this.addressService = addressService;
    }

    public String getAllContactInfos() {
//        contactInfos = service.findAllWithDepth("addresses");
        contactInfos = service.list(page, size);
        return SUCCESS;
    }
    public String getBulkContactInfos() {
                try {
                    contactInfos = service.findAllWithDepth("addresses");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return SUCCESS;
            }
 public String getContactInfoAddresses() {
                try {
                    contactInfos = service.findAllWithDepth("addresses");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return SUCCESS;
            }

    public String list() {
        contactInfos = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForUpdate() {
        prep();
        if (contactInfo != null && contactInfo.getContactInfoId() != null) {
            contactInfo = service.getById(contactInfo.getContactInfoId());
        }
        return "success";
    }

    public String setUpForInsert() {
        prep();

        return "success";
    }


    public String update() {

        prep();

        if (!validationSuccessful()) {
            return "input";
        } else {
            if (contactInfo.getAddressIdList() != null && contactInfo.getAddressIdList().length > 0) {
                        contactInfo.setAddresses(addressService.getByIdList(contactInfo.getAddressIdList()));
                    }
                    service.update(contactInfo);

            }


        return "success";
    }

    public String insert() {

        prep();

        if (!validationSuccessful()) {
            return "input";
        } else {

            if (contactInfo.getContactInfoId() == null) {
                service.create(contactInfo);

                if (contactInfo.getAddressIdList() != null && contactInfo.getAddressIdList().length > 0) {
                    contactInfo.setAddresses(addressService.getByIdList(contactInfo.getAddressIdList()));
                }

                service.update(contactInfo);

            }


        }
        return "success";
    }


    private void prep() {
        List<Integer> addressIdArrayList = addressService.getIdList();
        Map session = ActionContext.getContext().getSession();
        session.put("addressIdList", addressIdArrayList.toArray());
    }


    public String delete() {
        service.delete(contactInfo.getContactInfoId());
        return SUCCESS;
    }


    private boolean validationSuccessful() {
        if (contactInfo.getEmailId() == null || contactInfo.getEmailId().trim().length() < 1) {
            addActionMessage("Email Id is required");
        }
        if (contactInfo.getPhoneNo() == null || contactInfo.getPhoneNo().trim().length() < 1) {
            addActionMessage("Phone No is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }


    }

    public List<ContactInfo> getContactInfos() {
        return contactInfos;
    }

    public void setContactInfos(List<ContactInfo> contactInfos) {
        this.contactInfos = contactInfos;
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
}
